package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.graduate.domain.entity.MsDelog;
import com.ruoyi.graduate.domain.entity.MsPackageType;
import com.ruoyi.graduate.domain.entity.MsPhotographyOrder;
import com.ruoyi.graduate.domain.entity.MsPhotographyPackage;
import com.ruoyi.graduate.domain.param.*;
import com.ruoyi.graduate.domain.vo.MsOrderListVo;
import com.ruoyi.graduate.domain.vo.MsOrderPageVo;
import com.ruoyi.graduate.domain.vo.MsPhotographyOrderPageVo;
import com.ruoyi.graduate.domain.vo.MsUserOrderListVo;
import com.ruoyi.graduate.mapper.MsDelogMapper;
import com.ruoyi.graduate.mapper.MsPackageTypeMapper;
import com.ruoyi.graduate.mapper.MsPhotographyOrderMapper;
import com.ruoyi.graduate.mapper.MsPhotographyPackageMapper;
import com.ruoyi.graduate.mapping.MsPhotographyOrderMapping;
import com.ruoyi.graduate.mapping.MsPhotographyPackageMapping;
import com.ruoyi.graduate.service.MsPhotographyOrderService;
import com.ruoyi.graduate.utils.OrderNoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class MsPhotographyOrderServiceImpl extends ServiceImpl<MsPhotographyOrderMapper, MsPhotographyOrder>
implements MsPhotographyOrderService {

    @Autowired
    private MsPhotographyOrderMapper msPhotographyOrderMapper;

    @Autowired
    private MsPhotographyPackageMapper msPhotographyPackageMapper;

    @Autowired
    private MsPackageTypeMapper msPackageTypeMapper;

    @Autowired
    private MsDelogMapper msDelogMapper;

    @Override
    public PageInfo pageList(MsPhotographyOrderPageParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsPhotographyOrderPageVo> msPhotographyOrderPageVos = msPhotographyOrderMapper.pageList(param);

        return new PageInfo(msPhotographyOrderPageVos);
    }

    @Override
    public AjaxResult detail(Integer orderId) {

        return AjaxResult.success(msPhotographyOrderMapper.detail(orderId));
    }

    @Override
    public PageInfo photoPageList(MsOrderPageParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsOrderPageVo> msOrderPageVos = msPhotographyOrderMapper.photoOrderList(param);


        return new PageInfo(msOrderPageVos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult photoRecevive(MsPhotographyOrderReceiveParam param) {

        UpdateWrapper uw = new UpdateWrapper<>();
        uw.eq("order_id", param.getOrderId());
        uw.set("order_status", param.getOrderStatus());

        int update = msPhotographyOrderMapper.update(null, uw);

        MsPhotographyOrder byId = msPhotographyOrderMapper.selectById(param.getOrderId());

        MsDelog delog = new MsDelog();
        delog.setTitle("订单状态变更");
        if (param.getOrderStatus() == 2){
            delog.setContent("您的订单" +byId.getOrderNo() + "状态为被接单");
            delog.setCustomerId(byId.getCustomerId());
            msDelogMapper.insert(delog);
        }else if (param.getOrderStatus() == 4){
            delog.setContent("您的订单" +byId.getOrderNo() + "状态为被取消接单");
            delog.setCustomerId(byId.getCustomerId());
            msDelogMapper.insert(delog);
        }






        return update > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult orderUserAdd(MsOrderUserAddParam param) {

        MsPhotographyOrder add = MsPhotographyOrderMapping.INSTANCE.add(param);
        System.out.println(param);
        add.setOrderNo(OrderNoGenerator.generateOrderNo());

        int insert = msPhotographyOrderMapper.insert(add);

        MsDelog delog = new MsDelog();
        delog.setTitle("订单创建");
        delog.setContent("您的订单" + add.getOrderNo() + "状态为待处理");
        delog.setCustomerId(add.getCustomerId());

        msDelogMapper.insert(delog);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult orderPackageAdd(MsOrderPackageAddParam param) {

        MsPhotographyOrder order = MsPhotographyOrderMapping.INSTANCE.addPackage(param);
        MsPhotographyPackage msPhotographyPackage = msPhotographyPackageMapper.selectById(param.getPackageId());

        System.out.println(msPhotographyPackage);
        MsPackageType msPackageType = msPackageTypeMapper.selectById(msPhotographyPackage.getTypeId());



        order.setOrderNo(OrderNoGenerator.generateOrderNo());
        order.setPrice(msPhotographyPackage.getPrice());
        order.setPhotographerId(msPhotographyPackage.getPhotographyId());
        order.setPrice(msPhotographyPackage.getPrice());
        order.setShootType(msPackageType.getTypeName());
        order.setShootTopic(msPhotographyPackage.getPackageName());
        order.setDeliveryMethod(msPhotographyPackage.getDeliveryMethod());

        int insert = msPhotographyOrderMapper.insert(order);

        MsDelog delog = new MsDelog();
        delog.setTitle("订单创建");
        delog.setContent("您的订单" + order.getOrderNo() + "状态为待处理");
        delog.setCustomerId(order.getCustomerId());

        msDelogMapper.insert(delog);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public List<MsUserOrderListVo> userList(Long userId) {

        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("customer_id", userId);
        qw.orderByDesc("create_time");
        List list = msPhotographyOrderMapper.selectList(qw);
        List list1 = MsPhotographyOrderMapping.INSTANCE.userList(list);
        return list1;
    }

    @Override
    public AjaxResult listVo(Long photographyId) {

        List<MsOrderListVo> msOrderListVos = msPhotographyOrderMapper.listVo(photographyId);

        return AjaxResult.success(msOrderListVos);
    }
}
